package utils

import (
	"fmt"
	"math/big"
	"regexp"
	"strconv"
)

// 采集返回的时间字符串转换为对应的秒
func convertSNMPTime(durationStr string) (int64, error) {
	// 正则表达式匹配小时、分钟和秒
	re := regexp.MustCompile(`(?:(\d+)h)?(?:(\d+)m)?(?:(\d+(\.\d+)?)s)?`)
	matches := re.FindStringSubmatch(durationStr)

	if matches == nil {
		return 0, fmt.Errorf("invalid duration format")
	}

	var totalSeconds int64

	// 解析小时
	if matches[1] != "" {
		hours, err := strconv.Atoi(matches[1])
		if err != nil {
			return 0, err
		}
		totalSeconds += int64(hours * 3600)
	}

	// 解析分钟
	if matches[2] != "" {
		minutes, err := strconv.Atoi(matches[2])
		if err != nil {
			return 0, err
		}
		totalSeconds += int64(minutes * 60)
	}

	// 解析秒
	if matches[3] != "" {
		seconds, err := strconv.ParseInt(matches[3], 0, 64)
		if err != nil {
			return 0, err
		}
		totalSeconds += seconds
	}

	return totalSeconds, nil
}

func GetSize(size int64) string {
	GB := big.NewInt(1024 * 1024 * 1024)
	MB := big.NewInt(1024 * 1024)
	KB := big.NewInt(1024)

	var resultSize string

	if size >= GB.Int64() {
		resultSize = strconv.FormatInt(size/GB.Int64(), 10) + "GB"
	} else if size >= MB.Int64() {
		resultSize = strconv.FormatInt(size/MB.Int64(), 10) + "MB"
	} else if size >= KB.Int64() {
		resultSize = strconv.FormatInt(size/KB.Int64(), 10) + "KB"
	} else {
		resultSize = strconv.FormatInt(size, 10) + "B"
	}

	return resultSize
}
